﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReadNovel.Utils
{
    public static class Utils
    {
        public static bool IsPhoneNumber(string number)
        {
            return true;
        }

        public static string Unicode4HTML(string html)
        {
            StringBuilder sb = new StringBuilder();

            foreach (char c in html)
            {
                if (Convert.ToInt32(c) > 127)
                {
                    sb.Append("&#" + Convert.ToInt32(c) + ";");
                }
                else
                {
                    sb.Append(c);
                }
            }

            return sb.ToString();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="bNavigationLogin">是否跳转到登录窗口</param>
        /// <param name="callback">登录后回调函数，true:登录成功</param>
        /// <returns></returns>
        public static void ValidatIsLogin(Action<bool> callback, bool isFoward = false)
        {
            if (GloabalData.Instance.IsLogin)
            {
                callback(true);
            }
            else
            {
                new NavidateToLoginPageContext(callback, isFoward).NavigateToLogin();
            }
        }

        public static void Logout()
        {
            GloabalData.Instance.UserInfo = null;
        }

        /// <summary>
        /// 登录导航上下文
        /// </summary>
        private class NavidateToLoginPageContext
        {
            public Action<bool> CallBack { get; private set; }

            public bool IsFoward { get; private set; }

            Microsoft.Phone.Controls.PhoneApplicationFrame frame;

            public NavidateToLoginPageContext(Action<bool> callback, bool isfoward)
            {
                this.CallBack = callback;
                this.IsFoward = isfoward;
            }

            public void NavigateToLogin()
            {
                frame = System.Windows.Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame;
                if (frame != null)
                {
                    frame.Navigated += frame_NavidateToLoginPage_Navigated;
                    frame.Navigate(new Uri("/View/User/LoginPage.xaml", UriKind.Relative));
                }
            }

            private void frame_NavidateToLoginPage_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
            {
                if (frame != null)
                {
                    frame.Navigated -= frame_NavidateToLoginPage_Navigated;
                    ILogin login = frame.Content as ILogin;
                    if (login != null)
                    {
                        login.LoginSuccessCallback = this.CallBack;
                        login.IsFoward = this.IsFoward;
                    }
                }
            }
        }
    }
}
